﻿namespace EAL.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;

    public class Product : ModelBase, ICsvSerializable
    {
        private Decimal price;
        private Int32 stock;
        private String name;

        [Key]
        public int Id { get; set; }

        [Required]
        public String Name
        {
            get { return name; }
            set 
            { 
                name = value;
                this.OnPropertyChanged(() => this.Name);
            }
        }

        public Decimal Price
        {
            get { return price; }
            set 
            {
                price = value;
                this.OnPropertyChanged(() => this.Price);
            }
        }

        public Int32 Stock
        {
            get { return stock; }
            set 
            { 
                stock = value;
                this.OnPropertyChanged(() => this.Stock);
            }
        }

        public string ToCsvLine()
        {
            return String.Format("{0};{1};{2}", this.Name, this.Price, this.Stock);
        }

        public ICsvSerializable FromCsvLine(string line)
        {
            String[] data = line.Split(";".ToCharArray());
            return new Product() { Name = data[0], Price = decimal.Parse(data[1]), Stock = int.Parse(data[2]) };
        }
    }
}
